#coding: utf8
import re
from pygeocoder import Geocoder
import time
from datetime import datetime
import urlparse
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview
from decimal import *

class SalirUtils(object):

    utils = None

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            restaurant['reviews'] = []
            restaurant['city']=None
            restaurant['restaurant_name'] = hxs.select('//div[@id="datos-cabecera"]/h1/text()').extract()

            address0 = hxs.select('//div[@id="caracteristicas"]/p/span[@class="street-address"]/text()').extract()
            address1 = hxs.select('//div[@id="caracteristicas"]/p/span[@class="locality"]/text()').extract()
            address = self.utils.convertListToString(address0) + "," + self.utils.convertListToString(address1)
            restaurant['address']= address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['cp'] = hxs.select('//div[@id="caracteristicas"]/p/span[@class="postal-code"]/text()').extract()
            restaurant['score'] = self.__getScore(hxs.select('//span[@class="average rating-estrellas hidden"]/text()').extract(),1)
            restaurant['reviews_total'] = hxs.select('//div[@id="datos-cabecera"]/div[@class="valoraciones"]/a[@href="#item-opiniones-anchor"]/span/text()').extract()

            """list of types of restaurant"""
            restaurant['rest_type']=self.__getRestType(hxs.select('//p[@class="categorias"]'))

            web = hxs.select('//*[text()="web"]/@href').extract()
            if len(web) == 0:
                web = hxs.select('//*[text()="Web"]/@href').extract()
            if len(web) == 1:
                restaurant['url_rest']=web[0]

            fb = hxs.select('//*[text()="Facebook"]/@href').extract()
            """Caution: there are 2 fb accounts, first is for restaurant second is for salir.com"""
            if len(fb) == 2:
                restaurant['facebook'] = fb[0]
            tw = hxs.select('//*[text()="Twitter"]/@href').extract()
            if len(tw) == 2:
                restaurant['twitter'] = tw[0]

            restaurant['url_rest']=None
            return


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)

    def fillRestaurantCommentLeerMas(self,response,restaurant):
        try:

            """
            from scrapy.shell import inspect_response
            inspect_response(response)
            """


            hxs = HtmlXPathSelector(response)
            
            restaurantReview = RestaurantReview()
            restaurantReview['author'] = hxs.select('//div[@class="itemSingleOpinion"]/strong[@class="nombrefecha"]/a/text()').extract()
            """if none it's possible an anonymous"""
            if restaurantReview['author']==None or len(restaurantReview['author'])==0:
                restaurantReview['author'] = self.__getAuthorAnonymousLeerMas(self.utils.convertListToString(hxs.select('//div[@class="itemSingleOpinion"]/strong[@class="nombrefecha"]/em/text()').extract()))

            tmpDate = self.utils.convertListToString(hxs.select('//div[@class="itemSingleOpinion"]/strong[@class="nombrefecha"]/text()').extract())
            if tmpDate==None or len(tmpDate)==0:
                restaurantReview['date'] = self.__getCommentDateAnonymousLeerMas(self.utils.convertListToString(hxs.select('//div[@class="itemSingleOpinion"]/strong[@class="nombrefecha"]/em/text()').extract()))
            else:
                restaurantReview['date'] = self.__getCommentDateLeerMas(tmpDate)
            
            restaurantReview['score'] = self.__getScore(hxs.select('//div[@class="itemSingleOpinion"]/span/strong/text()').extract(),2)
            restaurantReview['title'] = hxs.select('//div[@class="itemSingleOpinion"]/h2/text()').extract()
            restaurantReview['comment'] = hxs.select('//div[@class="itemSingleOpinion"]/p[@class="txt-critica"]/text()').extract()
              
            restaurant['reviews'].append(restaurantReview)


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantCommentLeerMas en TRIP-UTILS from response: " + str(e),self.utils.ERROR)

    def __getCommentDateAnonymousLeerMas(self,txtDate):
        ret=None
        try:
            if len(txtDate)>0:
                txtDate = txtDate.replace("Anónimo","")
                txtDate = txtDate.strip()
                ret = self.__getCommentDateLeerMas(txtDate)
        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDateAnonymousLeerMas: " + str(e),self.utils.ERROR)
        finally:
            return ret


    def __getCommentDateLeerMas(self,txtDate):
        tmp_s=None
        txtDate=self.utils.cleanString(txtDate)
        try:
            if len(txtDate)>0:
                po=txtDate.find(":")
                if po != -1:
                    tmp_s=txtDate[:po-3]
                    date = datetime.strptime(tmp_s , '%d-%m-%Y')
                    tmp_s = date.strftime('%d/%m/%Y')
                    return tmp_s

        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDateLeerMas: " + str(e),self.utils.ERROR)
        

    def __getAuthorAnonymousLeerMas(self,txtAnonym):
        ret=None
        try:
            if len(txtAnonym)>0:
                if txtAnonym.find("Anónimo")!= -1:
                    ret='Anónimo'
        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e),self.utils.ERROR)
        finally:
            return ret


    def fillRestaurantComment(self,response,hxs,restaurant):
        try:

            """
            from scrapy.shell import inspect_response
            inspect_response(response)
            """
            
            
            restaurantReview = RestaurantReview()
            restaurantReview['author'] = hxs.select('div[@class="item"]/div[@class="cont"]/div[@class="box-cont clearfix"]/p[@class="nombrefecha"]/a/span[@class="reviewer"]/text()').extract()
            """if none it's possible an anonymous"""
            if restaurantReview['author']==None or len(restaurantReview['author'])==0:
                restaurantReview['author'] = hxs.select('div[@class="item"]/div[@class="cont"]/div[@class="box-cont clearfix"]/p[@class="nombrefecha"]/span[@class="reviewer"]/text()').extract()
                
            restaurantReview['date'] = self.__getCommentDate(self.utils.convertListToString(hxs.select('div[@class="item"]/div[@class="cont"]/div[@class="box-cont clearfix"]/div[@class="box-info"]/span/text()').extract()))
            
            restaurantReview['score'] = self.__getScore(hxs.select('div[@class="item"]/div[@class="cont"]/div[@class="box-cont clearfix"]/div[@class="box-info"]/div/span[@class="value"]/text()').extract(),1)            
            restaurantReview['title'] = hxs.select('div[@class="item"]/h3/a/text()').extract()
            restaurantReview['comment'] = hxs.select('div[@class="item"]/div[@class="cont"]/p[@class="description"]/text()').extract()
              
            restaurant['reviews'].append(restaurantReview)


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantComment en TRIP-UTILS from response: " + str(e),self.utils.ERROR)


    def __getCommentDate(self,txtDate):
        tmp_s=None
        try:
            if len(txtDate)>0:
                po=txtDate.find("-")
                if po != -1:
                    tmp_s=txtDate[po-2:]
                    date = datetime.strptime(tmp_s , '%d-%m-%Y')
                    tmp_s = date.strftime('%d/%m/%Y')
                    return tmp_s

        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e),self.utils.ERROR)


    def __getRestType(self,hxs):
        try:
            d=dict()
            for rest_type in hxs.select('a/text()').extract():
                d[self.utils.convertListToString(rest_type).strip()]=0
            return d.keys()      

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)

            
    def __getScore(self,txtScore,div):
        tmp_s=""
        if len(txtScore)>0:
            tmp_s = str(Decimal(txtScore[0])/div)
            """tmp_s = str(Decimal(tmp_s)*2)"""
        return tmp_s              


        